package com.todod.base.module.base.controller.admin.device;

import com.todod.base.module.base.controller.admin.dto.ReportDataReq;
import com.todod.base.module.base.controller.admin.dto.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.device.vo.*;
import com.todod.base.module.base.dal.dataobject.device.DeviceDO;
import com.todod.base.module.base.service.device.DeviceService;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Tag(name = "管理后台 - 仪器设备")
@RestController
@RequestMapping("/base/device")
@Validated
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @PostMapping("/create")
    @Operation(summary = "创建仪器设备")
    public CommonResult<Long> createDevice(@Valid @RequestBody DeviceSaveReqVO createReqVO) {
        return success(deviceService.createDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仪器设备")
    public CommonResult<Boolean> updateDevice(@Valid @RequestBody DeviceSaveReqVO updateReqVO) {
        deviceService.updateDevice(updateReqVO);
        return success(true);
    }

    @PutMapping("/start")
    @Operation(summary = "开始仪器设备")
    public CommonResult<Boolean> startDevice(@Valid @RequestBody DeviceSaveReqVO updateReqVO) {
        deviceService.startDevice(updateReqVO);
        return success(true);
    }

    @PutMapping("/stop")
    @Operation(summary = "暂停仪器设备")
    public CommonResult<Boolean> stopDevice(@Valid @RequestBody DeviceSaveReqVO updateReqVO) {
        deviceService.stopDevice(updateReqVO);
        return success(true);
    }


    @GetMapping("/enable")
    @Operation(summary = "启用仪器设备")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> enableDevice(@RequestParam("id") Long id) {
        log.info("调用启用仪器设备控制层");
        deviceService.enableDevice(id);
        return success(true);
    }

    @GetMapping("/disable")
    @Operation(summary = "禁用仪器设备")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> disDevice(@RequestParam("id") Long id) {
        log.info("调用禁用仪器设备控制层");
        deviceService.disableDevice(id);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仪器设备")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDevice(@RequestParam("id") Long id) {
        deviceService.deleteDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仪器设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DeviceRespVO> getDevice(@RequestParam("id") Long id) {
        DeviceDO device = deviceService.getDevice(id);
        return success(BeanUtils.toBean(device, DeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仪器设备分页")
    public CommonResult<PageResult<DeviceRespVO>> getDevicePage(@Valid DevicePageReqVO pageReqVO) {
        PageResult<DeviceDO> pageResult = deviceService.getDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仪器设备 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceExcel(@Valid DevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceDO> list = deviceService.getDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仪器设备.xlsx", "数据", DeviceRespVO.class,
                        BeanUtils.toBean(list, DeviceRespVO.class));
    }



    @GetMapping(value = "/downTemplate")
    @Operation(summary = "下载导入模版")
    public void problemTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        deviceService.problemTemplate(request, response);
    }
    @PostMapping(value = "/uploadFile")
    @Operation(summary = "上传导入文件")
    public ResultData uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        return deviceService.uploadFile(file);
    }
    @PostMapping(value = "/reportDepartment")
    @Operation(summary = "执行导入文件")
    public ResultData reportDepartment(@RequestBody @Validated ReportDataReq reportDataReq) throws IOException {
        return deviceService.reportDepartment(reportDataReq);
    }
    @PostMapping(value = "/downloadWrongReport")
    @Operation(summary = "下载导入失败错误报告")
    public void downloadWrongReport(HttpServletResponse response, @RequestBody @Validated ReportDataReq reportDataReq) throws IOException {
        deviceService.downloadWrongReport(response, reportDataReq.getFileUrl());
    }

}